package com.ruoyi.common.log.controller;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.log.config.Result;
import com.ruoyi.common.log.domain.*;
import com.ruoyi.common.log.service.*;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("crop")
public class CropController {
    @Autowired
    SysCropService CropService;
    @Autowired
    SysPlantationService plantationService;
    @Autowired
    SysUserService userService;
    @Autowired
    PlantingMethodService plantingMethodService;
    @Autowired
    CityService cityService;

    @PostMapping("list")
    public Result crop(@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "3")  Integer size, @RequestBody SysCrop crop){
        PageHelper.startPage(current, size);
        List<SysCrop> list = CropService.listAll(crop);
        PageInfo<SysCrop> sysCropPageInfo = new PageInfo<>(list);
        return Result.success(sysCropPageInfo);
    }
    @RequestMapping("plantation")
    public Result plantation(){
        List<SysPlantation> list = plantationService.list();
        return Result.success(list);
    }
    @RequestMapping("user")
    public Result user(){
        List<SysUser> list = userService.list();
        return Result.success(list);
    }
    @RequestMapping("plantingMethod")
    public Result plantingMethod(){
        List<PlantingMethod> list =plantingMethodService.list();
        return Result.success(list);
    }
    @RequestMapping("add")
    public Result add(@RequestBody SysCrop sysCrop){
        sysCrop.setCreateTime(new Date());
        String snowflakeNextIdStr = IdUtil.getSnowflakeNextIdStr();
        sysCrop.setCropEncoding(snowflakeNextIdStr);
        boolean save = CropService.save(sysCrop);
        return save? Result.success():Result.fail();
    }

}
